package com.ld.security.server.admin.modules.ge.vo.map;

import com.ld.security.common.core.base.SuperVo;
import com.ld.security.common.mybatis.anno.QueryKey;
import com.ld.security.common.mybatis.anno.SqlKey;
import com.ld.security.server.admin.modules.ge.entity.GeLinkEntity;
import com.ld.security.server.admin.modules.ge.po.DataSourcePo;
import lombok.Data;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Data
public class GeLinkMapVo extends DataSourcePo implements SuperVo {


    @QueryKey(groupKey = "tableNameX")
    @SqlKey(name = "dataSourceId",valueField = "dataSourceId")
    private Map<String,List<GeLinkEntity>> linkMap;

    /**
     * 计算2个节点的链接
     * @param tableX
     * @param tableY
     * @return
     */
    private boolean compLink(String tableX,String tableY,List<GeLinkEntity> path){
        for (GeLinkEntity geLinkEntity : linkMap.get(tableX)) {
          if(geLinkEntity.getTableNameY().equals(tableY)){
              path.add(geLinkEntity);
              return true;
          }
        }
        for (GeLinkEntity geLinkEntity : linkMap.get(tableX)) {
            boolean b = compLink(geLinkEntity.getTableNameY(),tableY,path);
            if(b){
                path.add(geLinkEntity);
                return true;
            }
        }
        return  false;
    }


    public List<GeLinkEntity> compLink(String tableX,String tableY){
        ArrayList<GeLinkEntity> list =    new ArrayList<>();
        ArrayList<GeLinkEntity> path =    new ArrayList<>();
        this.compLink(tableX,tableY,list);

        for (GeLinkEntity o : list) {
            path.add(0,o);
        }
        return path;
    }

}
